<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/webjars/jquery/3.6.0/dist/jquery.js"></script>
    <script src="/webjars/axios/0.26.1/dist/axios.js"></script>
    <style>
        table {
            border: black solid 1px;
            text-align: center;
            width: 800px;
        }
    </style>
</head>
<body>
<div id="app">
    <form action="" onsubmit="return false">
        <input type="hidden" name="id" th:value="${student.id}">
        <table align="center">
            <tr>
                <td>姓名</td>
                <td>
                    <input type="text" name="name" placeholder="请输入学生姓名" th:value="${student.name}">
                </td>
            </tr>
            <tr>
                <td>性别</td>
                <td>
                    男<input type="radio" name="sex" value="男" th:checked="${student.sex} eq '男'">&nbsp;&nbsp;
                    女<input type="radio" name="sex" value="女" th:checked="${student.sex} eq '女'">
                </td>
            </tr>
            <tr>
                <td>爱好</td>
                <td>
                    <input id="basketball" class="hobby" type="checkbox" name="hobby" value="篮球">
                    <label for="basketball">篮球</label>

                    <input id="football" class="hobby" type="checkbox" name="hobby" value="足球">
                    <label for="football">足球</label>

                    <input id="badminton" class="hobby" type="checkbox" name="hobby" value="羽毛球">
                    <label for="badminton">羽毛球</label>

                    <input id="tennis" class="hobby" type="checkbox" name="hobby" value="网球">
                    <label for="tennis">网球</label>
                </td>
            </tr>
            <tr>
                <td>课程</td>
                <td>
                    <span th:each="c:${courses}">
                        <input th:id="${c.id}" class="course" type="checkbox" name="courses"
                               th:value="${c.id}">
                        <label th:for="${c.id}" th:text="${c.name}"></label>
                    </span>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <button onclick="update()">【修改】</button>
                </td>
            </tr>
        </table>
    </form>
</div>
<script th:inline="javascript">
    $(function () {
        let hobby = [[${student.hobby}]]
        let courses = [[${student.courses}]]

        let h = hobby.split(',');
        $('.hobby').each((index, e) => {
            h.forEach(item => {
                if (e.value === item) {
                    $('#' + e.id).attr("checked", true)
                }
            })
        })
        $('.course').each((index, a) => {
            courses.forEach((c) => {
                if (a.id == c.id) {
                    $('#' + a.id).attr("checked", true)
                }
            })
        })
    })
    function update(){
        let formDate = $('form').serialize();

        axios.put('/stu/update', formDate).then(resp => {
            location.href='/stu/list'
        }).catch((e)=>{
            console.log(e);
        })
    }
</script>
</body>
</html>